package com.iyitong.common;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class BaseAction extends ActionSupport {

	private static final long serialVersionUID = 8666686001683556882L;

	public String getParameter(String parameterName) {
		return getRequest().getParameter(parameterName);
	}
	
	public void writeJosnObject(JSONObject json) {
		try {
			getResponse().setContentType("text;charset=UTF-8");
	        PrintWriter outwriter = getResponse().getWriter();
	        outwriter.print(json);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void writeString(String msg) {
		try {
			getResponse().setContentType("text;charset=UTF-8");
			PrintWriter outwriter = getResponse().getWriter();
			outwriter.print(msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public ActionContext getContext() {
		return ActionContext.getContext();
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}
}
